package shopping;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class ProductsTest {
    public static void main(String[] args) {
        List<AddCart> items = new ArrayList<>();
        Scanner sc = new Scanner(System.in);

        Products[] arr = new Products[5];
        Products pro1 = new Products("雪碧", "饮料", 3.0, 10);
        Products pro2 = new Products("可乐", "饮料", 3.0, 5);
        Products pro3 = new Products("果汁", "饮料", 5.0, 10);
        Products pro4 = new Products("苹果", "水果", 1.0, 10);
        Products pro5 = new Products("香蕉", "水果", 1.0, 10);
        arr[0] = pro1;
        arr[1] = pro2;
        arr[2] = pro3;
        arr[3] = pro4;
        arr[4] = pro5;


        while (true) {
            System.out.println("请选择以下操作：");
            System.out.println("1.查看商品信息");
            System.out.println("2.添加心仪商品至购物车");
            System.out.println("3.删除某商品");
            System.out.println("4.查看购物车");
            System.out.println("5.结算商品");
            System.out.println("6.结束购物");

            int choice = sc.nextInt();

            switch (choice) {

                //查看商品信息
                case 1:
                    for (int i = 0; i < arr.length; i++) {
                        Products pro = arr[i];
                        System.out.printf("商品名称：%s, 种类：%s, 单价：%.2f元, 库存：%d件\n",
                                pro.getName(), pro.getKind(), pro.getPrice(), pro.getCount());
                    }
                    break;

                //添加心仪商品至购物车
                case 2:
                    System.out.println("请输入你想选择的商品");
                    String name = sc.next();
                    int a = 0;
                    Products p = null;
                    for (int i = 0; i < arr.length; i++) {
                        if (arr[i].getName().equals(name)) {
                            p = arr[i];
                            a = 1;
                            System.out.printf("商品名称：%s, 种类：%s, 单价：%.2f元, 库存：%d件\n",
                                    p.getName(), p.getKind(), p.getPrice(), p.getCount());
                            break;
                        }
                    }

                    if (a == 0) {
                        System.out.println("未找到该商品，请重新输入！");
                        break;
                    }

                    System.out.println("输入你想要购买的数量：");
                    int count = sc.nextInt();
                    if (count > p.getCount()) {
                        System.out.println("库存不足，请重新输入！");

                    } else {
                        p.setCount(p.getCount() - count);
                        items.add(new AddCart(p, count));
                        System.out.println("已成功添加至购物车");
                    }

                    break;

                //删除某商品
                case 3:
                    System.out.println("输入你想删除的商品");
                    String name2 = sc.next();
                    for (int i = 0; i < items.size(); i++) {
                        AddCart item = items.get(i);
                        if (item.getProduct().getName().equals(name2)) {

                            for (int j = 0; j< arr.length; j++) {
                                Products product = arr[j];
                                if (product.getName().equals(name2)) {
                                    product.setCount(product.getCount() + item.getQuantity());
                                    break;
                                }
                            }

                            items.remove(i);
                            System.out.println("成功移除" );
                        }

                        break;
                    }

                //查看购物车
                case 4:
                    for (int i = 0; i < items.size(); i++) {
                        AddCart item = items.get(i);
                        double subtotal = item.getProduct().getPrice() * item.getQuantity();
                        System.out.printf(" 商品名称：%s, 种类：%s, 单价：￥%.2f, 数量：%d, 小计：￥%.2f\n",
                                item.getProduct().getName(), item.getProduct().getKind(),
                                item.getProduct().getPrice(), item.getQuantity(), subtotal);


                    }
                    break;


                //结算商品
                case 5:
                    double total = 0.0;
                    for (int i = 0; i < items.size(); i++) {
                        AddCart item = items.get(i);
                        total += item.getProduct().getPrice() * item.getQuantity();
                    }
                    System.out.printf("总金额为：￥%.2f\n", total);
                    break;


                //结束购物
                case 6:
                    System.out.println("欢迎下次光临！");
                    return;

                default:
                    System.out.println("错误的输入！");
            }
        }
    }
}




